//
//  UIButton+YJImagePosition.m
//  YJBaseProject
//
//  Created by 冯垚杰 on 2017/8/19.
//  Copyright © 2017年 冯垚杰. All rights reserved.
//

#import "UIButton+YJImagePosition.h"
#import <objc/runtime.h>

@implementation UIButton (YJImagePosition)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        method_exchangeImplementations(class_getInstanceMethod([self class], @selector(layoutSubviews)),
                                       class_getInstanceMethod([self class], @selector(YJImagePosition_layoutSubviews)));
    });
}

- (void)YJImagePosition_layoutSubviews
{
    [self YJImagePosition_layoutSubviews];
    if (self.yj_imagePosition != YJButtonImagePositionNone) {
        [self yj_updateImageLabelEdgeInsets];
    }
}

- (void)yj_updateImageLabelEdgeInsets
{
    CGFloat halfMargin = self.yj_imageTitleMargin / 2.0;
    CGFloat imageWith = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    CGFloat labelWidth = self.titleLabel.frame.size.width;
    CGFloat labelHeight = self.titleLabel.frame.size.height;
    switch (self.yj_imagePosition) {
        case YJButtonImagePositionLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -halfMargin, 0, halfMargin);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, halfMargin, 0, -halfMargin);
            break;
        case YJButtonImagePositionRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + halfMargin, 0, -labelWidth - halfMargin);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWith - halfMargin, 0, imageWith + halfMargin);
            break;
        case YJButtonImagePositionTop:
            self.imageEdgeInsets = UIEdgeInsetsMake(-labelHeight - halfMargin, 0, 0, -labelWidth);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight - halfMargin, 0);
            break;
        case YJButtonImagePositionBottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight - halfMargin, -labelWidth);
            self.titleEdgeInsets = UIEdgeInsetsMake(-imageHeight - halfMargin, -imageWith, 0, 0);
            break;
        default:
            break;
    }
}

#pragma mark - getters & setters

- (YJButtonImagePosition)yj_imagePosition
{
    NSNumber *number = objc_getAssociatedObject(self, @selector(yj_imagePosition));
    return number.integerValue;
}

- (void)setYj_imagePosition:(YJButtonImagePosition)yj_imagePosition
{
    if (self.yj_imagePosition == yj_imagePosition) {
        return;
    }
    if (yj_imagePosition == YJButtonImagePositionNone) {
        self.titleEdgeInsets = UIEdgeInsetsZero;
        self.imageEdgeInsets = UIEdgeInsetsZero;
    }
    objc_setAssociatedObject(self, @selector(yj_imagePosition),
                             @(yj_imagePosition),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setNeedsLayout];
}

- (CGFloat)yj_imageTitleMargin
{
    NSNumber *number = objc_getAssociatedObject(self, @selector(yj_imageTitleMargin));
    return number.floatValue;
}

- (void)setYj_imageTitleMargin:(CGFloat)yj_imageTitleMargin
{
    if (self.yj_imageTitleMargin == yj_imageTitleMargin) {
        return;
    }
    objc_setAssociatedObject(self, @selector(yj_imageTitleMargin),
                             @(yj_imageTitleMargin),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setNeedsLayout];
}

#pragma end

@end

